package administrator;

import java.awt.EventQueue;

public class AddQues {

	public JFrame frame;
	public JButton btnDone;
	public JButton btnNext;
	public JTextPane textPane; 
	public int counter = -1;
	public JLabel Header;
	private String QID = "";
	private String Question = "";
	private String OptA ="";
	private String OptB ="";
	private String OptC ="";
	private String OptD ="";
	private String SolOpt = "";
	private String Ans = "";
	private JLabel MaxChar;
	private JButton Cancelbtn;
	private JButton btnBack;
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddQues window = new AddQues();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	
	public AddQues() {
		initialize();
	}

	
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(375, 180, 500, 400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
	
		
		btnDone = new JButton("Done");
		btnDone.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				
				btnDoneActionPerformed(evt);
			}
		});
		btnDone.setBounds(201, 328, 89, 23);
		frame.getContentPane().add(btnDone);
		btnDone.setVisible(false);
		
		textPane = new JTextPane();
		textPane.setFont(new Font("Calibri", Font.PLAIN, 12));
		textPane.setBounds(144, 100, 200, 50);
		frame.getContentPane().add(textPane);
		
		
		Header = new JLabel("Enter the Question ID");
		Header.setHorizontalAlignment(SwingConstants.CENTER);
		Header.setBounds(10, 34, 464, 33);
		frame.getContentPane().add(Header);
		
	    btnNext = new JButton("Next");
		btnNext.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				
				btnNextActionPerformed(evt);
			}
		});
		btnNext.setBounds(201, 255, 89, 23);
		frame.getContentPane().add(btnNext);
		
		MaxChar = new JLabel("Max 10 Digits/Charaters");
		MaxChar.setFont(new Font("Tahoma", Font.PLAIN, 12));
		MaxChar.setHorizontalAlignment(SwingConstants.CENTER);
		MaxChar.setBounds(240, 229, 234, 15);
		frame.getContentPane().add(MaxChar);
		
		Cancelbtn = new JButton("Cancel");
		Cancelbtn.setBounds(201, 328, 89, 23);
		frame.getContentPane().add(Cancelbtn);
		
		btnBack = new JButton("Back");
		btnBack.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				
				btnBackActionPerformed(evt);
			}
		});
		btnBack.setBounds(201, 294, 89, 23);
		frame.getContentPane().add(btnBack);
		btnBack.setVisible(false);
		
		Cancelbtn.setVisible(true);
		Cancelbtn.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				
				CancelbtnActionPerformed(evt);
			}
		});
	}
	
	private void btnBackActionPerformed(java.awt.event.ActionEvent evt)
	{
		if(counter < -1)
		{
			counter = -1;
		}
		counter = counter -1;
		if(counter == -1)
		{
			btnDone.setVisible(false);
			Header.setText("Enter the QID");
			//QID = textPane.getText();
			textPane.setBounds(144, 100, 200, 50);
			MaxChar.setText("Max 10 Digits/Characters");
			textPane.setText(QID);
			btnBack.setVisible(false);
		}
		if( counter == 0)
		{
			btnDone.setVisible(false);
			Header.setText("Enter the Question");
			//QID = textPane.getText();
			textPane.setBounds(10, 121, 464, 96);
			MaxChar.setText("Max 500 Character");
			textPane.setText(Question);
			btnBack.setVisible(true);
		}
		if( counter == 1)
		{
			btnDone.setVisible(false);
			Header.setText("Enter the Option/Choice A");
			//Question = textPane.getText();
			//textPane.setBounds(150, 201, 464, 96);
			MaxChar.setText("Max 100 Character");
			textPane.setText(OptA);
			
		}
		if( counter == 2)
		{
			btnDone.setVisible(false);
			Header.setText("Enter the Option/Choice B");
			//OptA = textPane.getText();
			MaxChar.setText("Max 100 Character");
			textPane.setText(OptB);
		}
		if( counter == 3)
		{
			btnDone.setVisible(false);
			Header.setText("Enter the Option/Choice C");
			//OptB= textPane.getText();
			textPane.setText(OptC);
		}
		if( counter == 4)
		{
			btnDone.setVisible(false);
			Header.setText("Enter the Option/Choice D");
			//OptC = textPane.getText();
			textPane.setBounds(10, 121, 464, 96);
			MaxChar.setText("Max 100 Character");
			textPane.setText(OptD);
		}
		if( counter == 5)
		{
			btnDone.setVisible(false);
			Header.setText("Enter the Solution (Option/Choice)");
			//OptD = textPane.getText();
			textPane.setBounds(144, 100, 200, 50);
			MaxChar.setText("Max 1 Character");
			textPane.setText(SolOpt);
		}
		if( counter == 6)
		{
			btnDone.setVisible(false);
			Header.setText("Enter the Solution");
			//SolOpt = textPane.getText();
			textPane.setBounds(10, 121, 464, 96);
			MaxChar.setText("Max 100 Character");
			textPane.setText(Ans);
			textPane.setVisible(true);
			btnNext.setVisible(true);
		}
		if( counter == 7)
		{
			//Ans = textPane.getText();
			Cancelbtn.setVisible(false);
			btnDone.setVisible(true);
			btnNext.setVisible(true);
			Header.setText("");
			textPane.setText("");
			textPane.setVisible(true);
			MaxChar.setText("");
		}
	}
	
	
	
	
	
	private void btnNextActionPerformed(java.awt.event.ActionEvent evt) 
	{
		if(counter < -1)
		{
			counter = -1;
		}
		counter = counter +1;
		if( counter == 0)
		{
						
			QID = textPane.getText();
			
			if( QID == "" || QID.length() == 0)
			{
				JOptionPane.showMessageDialog(null, "Please enter QID \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
				
			}
			else if( QID.length() > 10 )
			{
				JOptionPane.showMessageDialog(null, " QID can not be more than 10 Digits/Character \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
				
			}
			else if ( QID == null )
			{
				JOptionPane.showMessageDialog(null, " Some internal error occured!!! Please try again  \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
			}
			else
			{
				textPane.setBounds(10, 121, 464, 96);
				MaxChar.setText("Max 500 Character");
				textPane.setText(Question);
				btnDone.setVisible(false);
				btnBack.setVisible(true);
				Header.setText("Enter the Question");
			}
		}
		
		if( counter == 1)
		{
			Question = textPane.getText();
			if(Question == "" || Question.length() == 0)
			{
				JOptionPane.showMessageDialog(null, "Please enter Question \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
				
			}
			else if( Question.length() > 500 )
			{
				JOptionPane.showMessageDialog(null, " Question length can not be more than 500 Character \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
				
			}
			else if ( Question == null )
			{
				JOptionPane.showMessageDialog(null, " Some internal error occured!!! Please try again  \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
			}
			else
			{
				btnDone.setVisible(false);
				btnBack.setVisible(true);
				Header.setText("Enter the Option/Choice A");
				MaxChar.setText("Max 100 Character");
				textPane.setText(OptA);
			}
		}
		if( counter == 2)
		{
			OptA = textPane.getText();
			if( OptA == "" || OptA.length() == 0)
			{
				JOptionPane.showMessageDialog(null, "Please enter Option/Choice A \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
				
			}
			else if( OptA.length() > 100 )
			{
				JOptionPane.showMessageDialog(null, " Length of Option/Choice can not be more than 100 Characters \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
				
			}
			else if ( OptA == null )
			{
				JOptionPane.showMessageDialog(null, " Some internal error occured!!! Please try again  \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
			}
			else
			{
				btnDone.setVisible(false);
				Header.setText("Enter the Option/Choice B");
				MaxChar.setText("Max 100 Character");
				textPane.setText(OptB);
			}
		}
		if( counter == 3)
		{
			OptB= textPane.getText();
			if( OptB == "" || OptB.length() == 0)
			{
				JOptionPane.showMessageDialog(null, "Please enter Option/Choice B \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
				
			}
			else if( OptB.length() > 100 )
			{
				JOptionPane.showMessageDialog(null, " Length of Option/Choice can not be more than 100 Characters \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
				
			}
			else if ( OptB == null )
			{
				JOptionPane.showMessageDialog(null, " Some internal error occured!!! Please try again  \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
			}
			else
			{
				btnDone.setVisible(false);
				Header.setText("Enter the Option/Choice C");
				textPane.setText(OptC);
			}
		}
		if( counter == 4)
		{
			OptC = textPane.getText();
			if( OptC == "" || OptC.length() == 0)
			{
				JOptionPane.showMessageDialog(null, "Please enter Option/Choice C \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
				
			}
			else if( OptC.length() > 100 )
			{
				JOptionPane.showMessageDialog(null, " Length of Option/Choice can not be more than 100 Characters \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
				
			}
			else if ( OptC == null )
			{
				JOptionPane.showMessageDialog(null, " Some internal error occured!!! Please try again  \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
			}
			else
			{
				btnDone.setVisible(false);
				Header.setText("Enter the Option/Choice D");
				MaxChar.setText("Max 100 Character");
				textPane.setText(OptD);
			}
		}
		if( counter == 5)
		{
			OptD = textPane.getText();
			if( OptD == "" || OptD.length() == 0)
			{
				JOptionPane.showMessageDialog(null, "Please enter Option/Choice D \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
				
			}
			else if( OptD.length() > 100 )
			{
				JOptionPane.showMessageDialog(null, " Length of Option/Choice can not be more than 100 Characters \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
				
			}
			else if ( OptD == null )
			{
				JOptionPane.showMessageDialog(null, " Some internal error occured!!! Please try again  \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
			}
			else
			{
				btnDone.setVisible(false);
				Header.setText("Enter the Solution (Option/Choice)");
				textPane.setBounds(144, 100, 200, 50);
				MaxChar.setText("Max 1 Character");
				textPane.setText(SolOpt);
			}
		}
		if( counter == 6)
		{
			SolOpt = textPane.getText();
			if( SolOpt == "" || SolOpt.length() == 0)
			{
				JOptionPane.showMessageDialog(null, "Please enter right Option/Choice \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
				
			}
			else if( SolOpt.length() > 1 )
			{
				JOptionPane.showMessageDialog(null, " Lenth of Solution Option can not be more than 1 Character \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
				
			}
			else if ( SolOpt == null )
			{
				JOptionPane.showMessageDialog(null, " Some internal error occured!!! Please try again  \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
			}
			else
			{
				btnDone.setVisible(false);
				Header.setText("Enter the Solution");
				textPane.setBounds(10, 121, 464, 96);
				MaxChar.setText("Max 100 Character");
				textPane.setText(Ans);
			}
		}
		if( counter == 7)
		{
			Ans = textPane.getText();
			if(Ans == null || Ans == "" || Ans.length() == 0)
			{
				JOptionPane.showMessageDialog(null, "Please enter Answer/Solution \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
				
			}
			else if( Ans.length() > 100 )
			{
				JOptionPane.showMessageDialog(null, " Length of Solution/Answer can not be more than 100 Characters \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
				
			}
			else if ( Ans == null )
			{
				JOptionPane.showMessageDialog(null, " Some internal error occured!!! Please try again  \n","Error", JOptionPane.ERROR_MESSAGE);
				counter = counter - 1;
			}
			else
			{
				
				Cancelbtn.setVisible(false);
				btnDone.setVisible(true);
				btnNext.setVisible(false);
				Header.setText("");
				textPane.setText(Ans);
				textPane.setVisible(false);
				MaxChar.setText("");
			}
		}
	}
	
	private void CancelbtnActionPerformed(java.awt.event.ActionEvent evt)
	{
		//System.out.close();
		this.frame.setVisible(false);
		QuesSetUp qs = new QuesSetUp();
		qs.frame.setVisible(true);
		
	}
	
	
	public int insertques()
	{
		int flag = 0;
		try
        {
			
			Class.forName("java.sql.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/oats","root","abcd");
            Statement stmt = con.createStatement();

            String sql="";
            sql = "insert into ques_paper values ( '"+QID+"','"+Question+"','"+OptA+"'";
            sql = sql + ",'"+OptB+"','"+OptC+"','"+OptD+"'";
            sql = sql + ",'"+Ans+ "','"+SolOpt + "');";
               
            stmt.execute(sql);
            stmt.close();
            con.close();
            flag = 0;
            return flag;
            
        }
        catch(Exception e)
        {
            System.out.println("Error is : "+e.getMessage());
            JOptionPane.showMessageDialog(null, e.getMessage() ,"Error", JOptionPane.ERROR_MESSAGE);
            flag = 1;
            return flag;
        }
		
	}
	
	private void btnDoneActionPerformed(java.awt.event.ActionEvent evt)
	{
		
		int flag = insertques();
		
		if(flag == 0)
		{
			JOptionPane.showMessageDialog(null, " Question was added successfully \n","Question Added", JOptionPane.PLAIN_MESSAGE);//JOptionPane.PLAIN_MESSAGE);
			this.frame.setVisible(false);
			QuesSetUp qs = new QuesSetUp();
			qs.frame.setVisible(true);
			
		}
		if(flag == 1)
		{
			JOptionPane.showMessageDialog(null, " No question was added due to errors  \n","Error", JOptionPane.ERROR_MESSAGE);//JOptionPane.PLAIN_MESSAGE);
			this.frame.setVisible(false);
			QuesSetUp qs = new QuesSetUp();
			qs.frame.setVisible(true);

		}
	}
}